<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>商品信息管理</title>	
	</head>
	<body>
		<center>
			<!-- 实现商品增删改查 -->
			<h3>商品信息管理</h3>
			<?php
				require("../../public/config.php");  //导入数据库配置文件
				require("../../public/functions.php");	//导入上传文件配置
				
				$path = "../../public/uploads/";	//设置上传文件的路径
				$typelist = array("image/png","image/jpg","image/gif","image/jpeg");	//设置上传文件的格式
				$upfile = $_FILES['pic'];	//获取上传文件的信息
				$pic = fileUpload($upfile,$path,$typelist);		//调用文件上传函数
				
				$link = @mysqli_connect(HOST,USER,PASS,DBNAME) or die("数据库连接失败！");	//连接数据库
				
				mysqli_set_charset($link,"utf8");		//设置数据库编码
				
				switch($_GET['a']){		//根据参数a的值进行相应的操作
					case "insert":
					//判断上传是否成功 并缩放
						if($pic['error']==false){
							echo "上传失败".$pic['info'];
						}else{
							//获取上传文件名
							$picname = $pic['info'];
							imageResize($picname,$path,100,100,$pre="s_");
							imageResize($picname,$path,400,400,$pre="m_");
						}
						//获取其他页面添加过来的信息
						$typeid = $_POST['typeid'];
						$goods = $_POST['goods'];
						$company = $_POST['company'];
						$price = $_POST['price'];
						$store = $_POST['store'];
						$descr = $_POST['descr'];
						$state = 1;//状态
						$num = 0;//购买数量
						$clicknum = 0;//点击量
						$addtime = time();
						//拼接sql语句
						$sql = "insert into goods(typeid,goods,company,price,store,descr,state,num,clicknum,addtime,picname) values('{$typeid}','{$goods}','{$company}','{$price}','{$store}','{$descr}','{$state}','{$num}','{$clicknum}','{$addtime}','{$picname}')";
						// echo $sql;
						//执行操作
						mysqli_query($link,$sql);
						//判断输出
						if(mysqli_insert_id($link)){
							echo '<script>alert("添加成功！"); </script>';
							header("refresh:0;url=index.php");
							exit();
						}else{
							echo '<script>alert("添加失败！"); </script>';
							header("refresh:0;url=index.php");
						}
						break;
					//执行删除操作
				  case "del":				
					   $id = $_GET['id'];
					   $picname = $_GET['picname'];
					   $sql = "delete from goods where id=".$id;
					   mysqli_query($link,$sql);
					   if(mysqli_affected_rows($link)>0){
						    @unlink($path.$picname);
							@unlink($path."m_".$picname);
							@unlink($path."s_".$picname);
						   echo "删除成功！";
						   header("Location:".$_SERVER["HTTP_REFERER"]);//删除成功后跳转到浏览时的位置
						   exit();
					   }else{
						   header("Location:".$_SERVER["HTTP_REFERER"]);
						   exit();
					   }
					   break;
					   
				case "update"://数据修改
	                	//判断是否有没有图片上传
	                	if($_FILES['pic']['error'] != 4){
	                		if($pic['error']){
	                			//获取上传文件名
	                			$picname = $pic['info'];
	                			imageResize($picname,$path,100,100,$pre="s_");
								imageResize($picname,$path,400,400,$pre="m_");
	                		}else{
	                			die("上传失败！".$pic['info']);
	                		}
	                	}else{
	                		$picname = $_POST['oldpicname'];
	                	}

	                	//获取修改信息
	                	$id = $_POST['id'];
	                	$typeid= $_POST['typeid'];
	                	$goods= $_POST['goods'];
	                	$company = $_POST['company'];
	                	$price = $_POST['price'];
	                	$store = $_POST['store'];
	                	$state = $_POST['state'];
	                	$descr = $_POST['descr'];
	                	//拼装sql语句
	                	$sql = "update goods set typeid='{$typeid}',goods='{$goods}',company='{$company}',price='{$price}',store='{$store}',state='{$state}',descr='{$descr}',picname='{$picname}' where id={$id}";
	                	// echo $sql;
	                	//执行
	                	mysqli_query($link,$sql);
	                	if(mysqli_affected_rows($link)>0){
	                		//判断有图片上传(删除原图)
	                		if($_FILES['pic']['error']==0){
	                			@unlink($path.$_POST['oldpicname']);
	                			@unlink($path."s_".$_POST['oldpicname']);
	                			@unlink($path."m_".$_POST['oldpicname']);
	                		}
	                		echo '<script>alert("修改成功！"); </script>';
							header("refresh:0;url=index.php");
							exit();
	                	}else{
	                		//判断有图片上传(删除新图)
	                		if($_FILES['pic']['error']==0){
	                			@unlink($path.$picname);
	                			@unlink($path."s_".$picname);
	                			@unlink($path."m_".$picname);
	                		}
							eader("Location:".$_SERVER["HTTP_REFERER"]);
							exit();
	                	}
	                	break;
	                	mysql_free_result($result);
						mysql_close($link);
				}
			?>
		</center>
	</body>
</html>